package com.lawyer.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lawyer.entity.CaseInfo;
import com.lawyer.entity.Cases;
import com.lawyer.entity.Config;
import com.lawyer.entity.DocumentShowConfig;
import com.lawyer.entity.Lawyer;
import com.lawyer.model.CaseInfoReq;
import com.lawyer.model.Message;
import com.lawyer.repository.CaseInfoRepository;
import com.lawyer.repository.CasesRepository;
import com.lawyer.repository.ConfigRepository;
import com.lawyer.repository.DocumentShowConfigRepository;
import com.lawyer.repository.HomePicRepository;
import com.lawyer.repository.LawyerRepository;
import com.lawyer.service.CaseInfoService;

@RestController
@RequestMapping(value = "/home")
@Api(tags = "首页相关")
@SuppressWarnings("all")
public class HomePageController {

	protected final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private CaseInfoService caseInfoService;
	@Autowired
	private HomePicRepository homePicRepository;

	@Autowired
	private CaseInfoRepository caseInfoRepository;
	@Autowired
	private CasesRepository casesRepository;
	@Autowired
	private LawyerRepository lawyerRepository;
	@Autowired
	private ConfigRepository configRepository;
	@Autowired
	private DocumentShowConfigRepository documentShowConfigRepository;

	@ApiOperation(httpMethod = "GET", value = "首页轮播广告", notes = "首页轮播广告")
	@RequestMapping(value = "/queryPagePic", method = RequestMethod.GET)
	public HttpEntity<?> queryPagePic() {
		Message msg = new Message();
		msg.setData(homePicRepository.findAll());
		return new ResponseEntity<>(msg, HttpStatus.OK);
	}

	@ApiOperation(httpMethod = "GET", value = "首页搜索", notes = "首页搜索")
	@ApiImplicitParams({ @ApiImplicitParam(name = "page", required = false, value = "第一页 page = 0", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "size", required = false, value = "分页条数", paramType = "query", dataType = "String") })
	@RequestMapping(value = "/queryByKeyWord", method = RequestMethod.GET)
	public HttpEntity<?> queryByKeyWord(String keyWord, @PageableDefault(page = 0, direction = Sort.Direction.DESC) Pageable pageable) {
		Message msg = new Message();
		Page<Lawyer> page = lawyerRepository.queryByKeyWord(keyWord, pageable);
		Config config = configRepository.findByName("ConsultingFee");// 律师咨询费
		for (Lawyer lawyer : page.getContent()) {
			lawyer.setConsultPrice(Double.valueOf(config.getValue()));
		}
		msg.setData(page);
		return new ResponseEntity<>(msg, HttpStatus.OK);
	}

	@ApiOperation(httpMethod = "GET", value = "分类快捷展示列表", notes = "分类快捷展示列表")
	@RequestMapping(value = "/quickShowCaseType", method = RequestMethod.GET)
	public HttpEntity<?> quickShowCaseType() {
		Message msg = new Message();
		Map<String, Object> result = new HashMap<String, Object>();
		List<Cases> list = casesRepository.findByQuickShow(true);
		List<DocumentShowConfig> document = documentShowConfigRepository.findAll();
		result.put("cases", list);
		result.put("document", document);
		msg.setData(result);
		return new ResponseEntity<>(msg, HttpStatus.OK);
	}

	@ApiOperation(httpMethod = "GET", value = "分类筛选页（大类）", notes = "分类筛选页（大类）")
	@RequestMapping(value = "/findCaseTypeList", method = RequestMethod.GET)
	public HttpEntity<?> findCaseTypeList() {
		Message msg = new Message();
		List<Cases> list = casesRepository.findAll();
		msg.setData(list);
		return new ResponseEntity<>(msg, HttpStatus.OK);
	}

	@ApiOperation(httpMethod = "GET", value = "分类筛选页（小类）", notes = "分类筛选页（大类）")
	@RequestMapping(value = "/findCaseInfoList", method = RequestMethod.GET)
	public HttpEntity<?> findCaseInfoList() {
		Message msg = new Message();
		List<CaseInfo> list = caseInfoRepository.findAll();
		msg.setData(list);
		return new ResponseEntity<>(msg, HttpStatus.OK);
	}

	@ApiOperation(httpMethod = "GET", value = "分类筛选页,通过分类,区域，评分，标签查询律师", notes = "分类筛选页,通过分类,区域，评分，标签查询律师")
	@ApiImplicitParams({ @ApiImplicitParam(name = "page", required = false, value = "第一页 page = 0", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "size", required = false, value = "分页条数", paramType = "query", dataType = "String") })
	@RequestMapping(value = "/queryLawyerPage", method = RequestMethod.GET)
	public HttpEntity<?> queryLawyerPage(CaseInfoReq caseInfoReq, @PageableDefault(page = 0, direction = Sort.Direction.DESC) Pageable pageable) {

		Message msg = caseInfoService.queryLawyerPage(caseInfoReq, pageable);
		return new ResponseEntity<>(msg, HttpStatus.OK);
	}
}
